﻿/*
 * Nix PBEM Game Engine.
 * Copyright (C) 2008, Lauris Bukšis-Haberkorns
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;

using Nix.PBeM.Engine.DataTypes.Orders;
using Nix.PBeM.Engine.DataTypes;
using Nix.PBeM.Engine.DataSources;

namespace Nix.PBeM.Engine.Process.Orders
{
	public class UnstackOrder : OrderBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UnstackOrder"/> class.
		/// </summary>
		public UnstackOrder ()
			: base("UNSTACK")
		{
			this.Syntax = this.Keyword;
			this.Description = "This order always executes. When a unit is in a stack, but not leading the entire stack, the unit departs from the stack. If leading a substack, it will become the leader of the new stack. If not in any stack, does nothing.";
			this.Type = OrderType.Immediate;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UnstackOrder"/> class.
		/// </summary>
		/// <param name="entity">The entity.</param>
		/// <param name="parser">The parser.</param>
		public UnstackOrder ( OrderEntity entity, StringLineParser parser )
			: this()
		{
			this.Entity = entity;
			this.Parser = parser;
		}

		/// <summary>
		/// Create order instance.
		/// </summary>
		/// <param name="entity">The entity.</param>
		/// <returns>Order instance.</returns>
		public override OrderBase Create ( OrderEntity entity, StringLineParser parser )
		{
			return new UnstackOrder(entity, parser);
		}

		public override void Parse ()
		{
			if ( this.Entity.GetType() != typeof(Unit) )
				throw new OrderParseException("This order can be specified only for unit.");
		}

		/// <summary>
		/// Order duration.
		/// </summary>
		/// <value></value>
		public override int Duration
		{
			get
			{
				return base.Duration;
			}
			set
			{
				// Allow setting only duration 1 or 0 when executed
				if ( value > 1 || value < 0 )
					base.Duration = 1;
				else
					base.Duration = value;
			}
		}

		public override void Execute ()
		{
			base.Execute();

			UnstackOrder.Unstack((Unit)this.Entity);

			this.CompleteExecution();
		}

		public static void Unstack ( Unit unit )
		{
			// If there is already leader, unstack
			if ( unit.Leader != null )
			{
				unit.Leader.AddEventMessage(string.Format("{0} unstacked.", unit));
				unit.AddEventMessage(string.Format("Unstacks from {0}.", unit.Leader));
			}
		}
	}
}
